package com.teamall.web.fpxm.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.teamall.core.common.JModelAndView;
import com.teamall.core.common.Page;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.util.DateUtils;
import com.teamall.framework.util.Utils;
import com.teamall.web.base.db.po.TownInfoPO;
import com.teamall.web.base.db.po.VillageInfoPO;
import com.teamall.web.base.service.ITownInfoService;
import com.teamall.web.base.service.IVillageInfoService;
import com.teamall.web.fpgz.db.po.CodeFamilyInfoPO;
import com.teamall.web.fpgz.db.po.CodeVillageProjectInfoPO;
import com.teamall.web.fpgz.service.ICodeVillageProjectInfoService;
import com.teamall.web.fpxm.db.po.AidProjectProgressPO;
import com.teamall.web.fpxm.db.po.CodeAidProject4listPO;
import com.teamall.web.fpxm.db.po.CodeAidProjectPO;
import com.teamall.web.fpxm.service.IAidProjectService;
import com.teamall.web.fpxm.service.ICodeAidProject4listService;
import com.teamall.web.fpxm.service.ICodeAidProjectService;
import com.teamall.web.fpxm.service.impl.AidProjectProgressServiceImpl;
import com.teamall.web.sys.db.po.DataDictPO;
import com.teamall.web.sys.service.IDataDictService;
import com.teamall.web.xtgl.service.IBasicDataService;

/**
 * ${DESCRIPTION}
 *
 * @author
 * @create 2017-07-30 14:09
 **/
@RequestMapping("/fpxm/")
@Controller
public class FpxmController {
    @Autowired
    private ITownInfoService townInfoService;

    @Autowired
    private IVillageInfoService villageInfoService;

    @Autowired
    private ICodeVillageProjectInfoService codeVillageProjectInfoService;

    @Autowired
    private AidProjectProgressServiceImpl aidProjectProgressService;

    @Autowired
    private IAidProjectService aidProjectService;

    @Autowired
    private ICodeAidProjectService codeAidProjectService;

    @Autowired
    private IDataDictService dataDictService;

    @Autowired
    private ICodeAidProject4listService codeAidProject4listService;

    @Autowired
    private IBasicDataService basicDataService;

    @RequestMapping("xmIndex")
    public ModelAndView xmIndex(Integer villageId, Integer townId,
            Integer qryType) throws BaseAppException {
        // 项目分类
        DataDictPO qryDataDict = new DataDictPO();
        qryDataDict.setDataGroup(300);
        List<DataDictPO> projectTypeList = dataDictService
                .selectByArg(qryDataDict);

        ModelAndView modelAndView = new JModelAndView("/fpxm/list");
        modelAndView.addObject("villageId", null == villageId ? "-1"
                : villageId);
        modelAndView.addObject("townId", null == townId ? "-1" : townId);
        modelAndView.addObject("projectTypeList", projectTypeList);
        modelAndView.addObject("qryType", null == qryType ? 1 : qryType);
        modelAndView.addObject("currentYear", DateUtils.date2String(new Date(),
                DateUtils.STR_DATE_FORMAT_YEAR));
        return modelAndView;
    }

    @RequestMapping("/list")
    public ModelAndView list() throws BaseAppException {
        ModelAndView modelAndView = new JModelAndView("/fpxm/fpxm_main");
        List<TownInfoPO> townInfoPOList = townInfoService
                .selectByArg(new TownInfoPO());
        List<VillageInfoPO> villageInfoPOList = villageInfoService
                .selectByArg(new VillageInfoPO());
        for (TownInfoPO townInfoPO : townInfoPOList) {
            for (VillageInfoPO villageInfoPO : villageInfoPOList) {
                if (townInfoPO.getTownId() == villageInfoPO.getTownId()) {
                    townInfoPO.getVillageInfoPOList().add(villageInfoPO);
                }
            }
        }
        modelAndView.addObject("townInfoPOList", townInfoPOList);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/qryProjectByPage")
    public Object qryProjectByPage(String projectName, Integer projectType,
            int rows, int page, Integer qryType, Integer townId,
            Integer villageId, String vtime) throws BaseAppException {

        if (1 == qryType) {
            //
            CodeAidProject4listPO po = new CodeAidProject4listPO();
            po.setProjectName(projectName);
            po.setProjectType(projectType);
            if (-999 < townId) {
                po.setProjectBelong(townId);
            }
            if (Utils.notEmpty(vtime)) {
                po.setProjectBeginDate(DateUtils.string2Date(vtime,
                        DateUtils.STR_DATE_FORMAT_YEAR));
            }
            Page<CodeAidProject4listPO> resultPage = new Page<CodeAidProject4listPO>();
            resultPage.setPage(page);
            resultPage.setRows(rows);
            codeAidProject4listService.selectByArgAndPage(po, resultPage);
            return resultPage;
        }
        else {
            CodeVillageProjectInfoPO po = new CodeVillageProjectInfoPO();
            po.setProjectName(projectName);
            po.setProjectType(projectType);
            po.setVillageId(villageId);
            if (Utils.notEmpty(vtime)) {
                po.setProjectBeginDate(DateUtils.string2Date(vtime,
                        DateUtils.STR_DATE_FORMAT_YEAR));
            }
            Page<CodeVillageProjectInfoPO> resultPage = new Page<CodeVillageProjectInfoPO>();
            resultPage.setPage(page);
            resultPage.setRows(rows);
            codeVillageProjectInfoService.selectByArgAndPage(po, resultPage);
            return resultPage;
        }

    }

    @RequestMapping("/detail")
    public ModelAndView detail(CodeAidProjectPO po) throws BaseAppException {
        ModelAndView modelAndView = new JModelAndView("/fpxm/detail");
        CodeAidProjectPO aidProjectPO = codeAidProjectService
                .selectByPrimaryKey(po.getProjectId());
        AidProjectProgressPO aidProjectProgressPO = new AidProjectProgressPO();
        aidProjectProgressPO.setProjectId(po.getProjectId());
        modelAndView.addObject("aidProjectPO", aidProjectPO);
        modelAndView.addObject("appList",
                aidProjectProgressService.selectByArg(aidProjectProgressPO));
        return modelAndView;
    }

    /**
     * 跳转到查看绑定的贫困户列表
     * 
     * @param po
     * @return
     * @throws BaseAppException
     */
    @RequestMapping("/familyRelated")
    public ModelAndView familyRelated(CodeAidProjectPO aidProjectPO)
            throws BaseAppException {
        ModelAndView modelAndView = new JModelAndView(
                "/fpxm/fpxm_main_familyRelated");
        // 镇
        List<TownInfoPO> townInfoPOList = townInfoService
                .selectByArg(new TownInfoPO());
        List<VillageInfoPO> villageInfoPOList = villageInfoService
                .selectByArg(new VillageInfoPO());
        for (TownInfoPO townInfoPO : townInfoPOList) {
            for (VillageInfoPO villageInfoPO : villageInfoPOList) {
                if (townInfoPO.getTownId() == villageInfoPO.getTownId()) {
                    townInfoPO.getVillageInfoPOList().add(villageInfoPO);
                }
            }
        }
        modelAndView.addObject("townInfoPOList", townInfoPOList);
        modelAndView.addObject("aidProjectPO", aidProjectPO);
        return modelAndView;
    }

}
